在 Swagger 中,你可以通過以下步驟來添加路由和參數。假設你正在使用 OpenAPI 规范(Swagger 2.0 或 3.0),以下是一些基本操作:
swagger: '2.0'
info:
title: Sample API
version: 1.0.0
paths:
/users:
get:
summary: Get all users
description: Returns a list of users
responses:
200:
description: A list of users
schema:
type: array
items:
$ref: '#/definitions/User'
definitions:
User:
type: object
properties:
id:
type: integer
name:
type: string
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
paths:
/users:
get:
summary: Get all users
description: Returns a list of users
responses:
'200':
description: A list of users
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
type: string
如果您希望為 GET /users
添加一個查詢參數,例如 age
:
paths:
/users:
get:
summary: Get all users
description: Returns a list of users
parameters:
- name: age
in: query
description: Age of the user to filter
required: false
type: integer
responses:
200:
description: A list of users
schema:
type: array
items:
$ref: '#/definitions/User'
paths:
/users:
get:
summary: Get all users
description: Returns a list of users
parameters:
- name: age
in: query
description: Age of the user to filter
required: false
schema:
type: integer
responses:
'200':
description: A list of users
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
以上就是在 Swagger 中添加路由和參數的基本操作。你可以根據需要擴展其他 HTTP 方法和參數類型(如路徑參數、請求體等)。這些基本結構可以幫助你開始構建和描述 API。